package com.sphd.restaurantsystem.fragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.GjAdapter;
import com.sphd.restaurantsystem.base.LazyLoadFragment;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.JiFenInfo;
import com.sphd.restaurantsystem.info.VipInfo;
import com.sphd.restaurantsystem.interfaces.IntegralCallBack;
import com.sphd.restaurantsystem.presenter.GetIntegralPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.GetIntegralView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/11/14.
 */

public class GetjfHistoryFragment extends LazyLoadFragment implements GetIntegralView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Bundle bundle;
    GetIntegralPresenterImpl getPresenter;
    GjAdapter gjAdapter;
    VipInfo vipInfo;
    IntegralCallBack integralCallBack;
    @Override
    public int getLayout() {
        return R.layout.swf_and_recy;
    }

    @Override
    public void initViews(View rootView) {
        ButterKnife.bind(this, rootView);
        getPresenter = new GetIntegralPresenterImpl(this);
        vipInfo = (VipInfo) bundle.getSerializable(Constant.VIPINFO);
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(this);
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        gjAdapter = new GjAdapter(R.layout.layout_jifen_item, new ArrayList<>());
        myRecy.setAdapter(gjAdapter);
    }

    @Override
    public void loadData() {
            addData();
    }

    private void addData() {
        getPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.GETJIFENHISTORY + "cgid=" +
                vipInfo.getMem_cgid() + "&mem_card=" +
                vipInfo.getMem_card() + "&mem_phone=" + vipInfo.getMem_phone() + "&status=1");
    }

    public void setVal(Bundle bundle,IntegralCallBack integralCallBack) {
        this.bundle = bundle;
        this.integralCallBack=integralCallBack;
    }

    @Override
    public void recRes(List<JiFenInfo> jiFenInfoList, int integral) {
        gjAdapter.setNewData(jiFenInfoList);
        integralCallBack.returnIntegral(integral);

    }

    @Override
    public void onRefresh() {
        addData();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }
}
